package cn.dada.dadarecipes.backend.merchant.controller;

import cn.dada.dadarecipes.backend.merchant.service.MerchantService;
import cn.dada.dadarecipes.base.response.JsonResult;
import cn.dada.dadarecipes.mall.product.pojo.dto.ProductAddParam;
import cn.dada.dadarecipes.mall.product.pojo.dto.ProductEditParam;
import cn.dada.dadarecipes.mall.product.pojo.dto.ProductSelectParam;
import cn.dada.dadarecipes.mall.shop.pojo.dto.ShopAddParam;
import cn.dada.dadarecipes.mall.shop.pojo.dto.ShopEditParam;
import cn.dada.dadarecipes.mall.shop.pojo.dto.ShopSelectParam;
import cn.dada.dadarecipes.user.order.pojo.dto.MerchantOrderUpdateParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

@Slf4j
@Validated
@Api(tags = "04.01商户后台管理")
@RestController
@RequestMapping("/v1/merchant/")
public class MerchantController {
    @Autowired
    private MerchantService merchantService;



    /**
     * 控制器-商家登录
     * @param username
     * @param password
     * @return
     */
    @PostMapping("login")
    @ApiOperation(value = "商家登录")
    public JsonResult merchantLogin(
            @NotBlank(message = "用户名不能为空") String username,
            @NotBlank(message = "密码不能为空") String password,
            @ApiIgnore HttpSession session) {
        log.debug("控制器-商家登录:username={},password={}", username, password);
        return JsonResult.ok(merchantService.merchantLogin(username, password, session));
    }

    /**
     * 控制器-商家退出登录
     * @param businessId
     * @param session
     * @return
     */
    @PostMapping("logout")
    @ApiOperation(value = "商家退出登录")
    public JsonResult merchantLogout(Long businessId, @ApiIgnore HttpSession session) {
        log.debug("控制器-商家退出登录:businessId={}", businessId);
        merchantService.merchantLogout(businessId, session);
        return JsonResult.ok();
    }

    /**
     * 控制器-添加店铺
     * @param shopAddParam
     * @param session
     * @return
     */
    @PostMapping("shop/add")
    @ApiOperation(value = "添加店铺")
    public JsonResult addShop(@Validated ShopAddParam shopAddParam, @ApiIgnore HttpSession session) {
        log.debug("控制器-添加店铺:shopAddParam={}", shopAddParam);
        merchantService.addShop(shopAddParam, session);
        return JsonResult.ok();
    }

    /**
     * 控制器-查询店铺
     * @param shopSelectParam
     * @param session
     * @return
     */
    @GetMapping("shop/select")
    @ApiOperation(value = "查询店铺")
    public JsonResult selectShop(@Validated ShopSelectParam shopSelectParam, @ApiIgnore HttpSession session) {
        log.debug("控制器-查询店铺:shopSelectParam={}", shopSelectParam);
        return JsonResult.ok(merchantService.selectShop(shopSelectParam, session));
    }

    /**
     * 控制器-编辑店铺
     * @param shopEditParam
     * @param session
     * @return
     */
    @PostMapping("shop/edit")
    @ApiOperation(value = "编辑店铺")
    public JsonResult editShop(ShopEditParam shopEditParam, @ApiIgnore HttpSession session) {
        log.debug("控制器-编辑店铺:shopEditParam={}", shopEditParam);
        merchantService.editShop(shopEditParam, session);
        return JsonResult.ok();
    }

    /**
     * 控制器-删除店铺
     * @param id
     * @param businessId
     * @param session
     * @return
     */
    @PostMapping("shop/drop")
    @ApiOperation(value = "删除店铺")
    public JsonResult dropShop(
            @NotNull(message = "店铺id不能为空") Long id,
            @NotNull(message = "商家id不能为空") Long businessId,
            @ApiIgnore HttpSession session) {
        log.debug("控制器-删除店铺:id={},businessId={}", id, businessId);
        merchantService.dropShop(id, businessId, session);
        return JsonResult.ok();
    }

    @PostMapping("product/add")
    @ApiOperation(value = "添加商铺商品")
    public JsonResult addProduct(@Validated ProductAddParam productAddParam, @ApiIgnore HttpSession session){
        log.debug("控制器-添加商铺商品:param={}", productAddParam);
        merchantService.addProduct(productAddParam, session);
        return JsonResult.ok();
    }

    @GetMapping("product/select")
    @ApiOperation(value = "查询商铺商品")
    public JsonResult selectProduct(@Validated ProductSelectParam productSelectParam, @ApiIgnore HttpSession session){
        log.debug("控制器-查询商铺商品:param={}", productSelectParam);
        return JsonResult.ok(merchantService.selectProduct(productSelectParam,session));
    }

    @PostMapping("product/edit")
    @ApiOperation(value = "编辑商铺商品")
    public JsonResult editProduct(@Validated ProductEditParam productEditParam, @ApiIgnore HttpSession session){
        log.debug("控制器-编辑商铺商品:param={}", productEditParam);
        merchantService.editProduct(productEditParam, session);
        return JsonResult.ok();
    }

    @PostMapping("product/drop")
    @ApiOperation(value = "删除商铺商品")
    public JsonResult dropProduct(@NotNull(message = "商品id不能为空") Long id,
                                  @NotNull(message = "商家id不能为空") Long businessId,
                                  @ApiIgnore HttpSession session){
        log.debug("控制器-删除商铺商品:id={},businessId={}", id, businessId);
        merchantService.dropProduct(id, businessId, session);
        return JsonResult.ok();
    }

    @GetMapping("order/select")
    @ApiOperation(value = "查询商铺商品订单")
    public JsonResult selectOrder(@NotNull(message = "商家id不能为空") Long businessId,Long shopId,
                                  @ApiIgnore HttpSession session){
        log.debug("控制器-查询商铺商品订单:businessId={},shopId={}", businessId, shopId);
        merchantService.selectOrder(businessId,shopId,session);
        return JsonResult.ok();
    }

    @PostMapping("order/update")
    @ApiOperation(value = "更新商铺商品订单")
    public JsonResult updateOrder(@Validated MerchantOrderUpdateParam param,@ApiIgnore HttpSession session){
        log.debug("控制器-更新商铺商品订单:param={}", param);
        merchantService.updateOrder(param,session);
        return JsonResult.ok();
    }
}
